#include "ScriptInterpreter.h"


ScriptInterpreter::ScriptInterpreter(void)
{
}


ScriptInterpreter::~ScriptInterpreter(void)
{
}

float getelap()
{
	return  (float)GetTickCount64() / (float)1000;
}

void ScriptInterpreter::OpenScript(char* file)
{
	std::fstream f(file);

	if(!f.is_open())
	{
		printf("Could not find script file: %s", file);
		return;
	}

	std::stringstream buf;
	buf << f.rdbuf();

	std::string s = buf.str();

	const char* srcString = s.c_str();

	f.close();

	this->script = new char[strlen(srcString)];
	strcpy(this->script, srcString);

	scan = new ScriptScanner(this->script);
}

void ScriptInterpreter::Start()
{
	this->startTime = getelap();
}

int ScriptInterpreter::Tick()
{
	float execTime = getelap() - this->startTime;

	scan->ReadOneLine();

	while(scan->NextFuncTime() <= execTime)
	{
		char* func = scan->NextFunc();
		float funcTime = scan->NextFuncTime();
		float funcArg = scan->NextFuncArg();

		printf("Exec: at: %f\tFunc: %s\tArg: %f\n", funcTime, func, funcArg);

		if(!strcmp(func, "return"))
			return 0;

		scan->hasExec();

		scan->ReadOneLine();
	}

	return 1;
}